<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 400px;
            border: 1px solid #000;
            border-collapse: collapse;
            margin: 0 auto;
        }
        
        td,
        th {
            border: 1px solid #000;
            text-align: center;
        }
        
        input {
            width: 50px;
        }
        
        .search {
            width: 600px;
            margin: 20px auto;
        }
    </style>
</head>

<body>
    <div class="search">
        按照价格查询: <input type="text" class="start"> - <input type="text" class="end"> <button class="search-price">搜索</button> 按照商品名称查询: <input type="text" class="product"> <button class="search-pro">查询</button>
    </div>
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>产品名称</th>
                <th>价格</th>
            </tr>
        </thead>
        <tbody>


        </tbody>
    </table>
    <script>
        var data = [{
            id: 1,
            pname: '小米',
            price: 1000
        }, {
            id: 2,
            pname: '华为',
            price: 2000
        }, {
            id: 3,
            pname: '苹果',
            price: 3000
        }]
        var tbody = document.querySelector('tbody');
        var start = document.querySelector('.start');
        var end = document.querySelector('.end');
        var search_price = document.querySelector('.search-price');
        var product = document.querySelector('.product');
        var search_pro = document.querySelector('.search-pro');
        setData(data);

        function setData(mydata) {
            tbody.innerHTML = '';
            mydata.forEach(function(value) {
                var tr = document.createElement('tr');
                tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>';
                tbody.appendChild(tr);
            })
        }
        search_price.addEventListener('click', function() {
            var newData = data.filter(function(value) {
                return value.price >= start.value && value.price <= end.value;
            })
            setData(newData);
        })
        search_pro.addEventListener('click', function() {
            var arr = [];
            data.some(function(value) {
                if (value.pname === product.value) {
                    arr.push(value);
                    return true;
                }
            })
            setData(arr);
        })
        product.addEventListener('keyup', function() {
            var result = data.filter(function(value) {
                if (value.pname.includes(product.value)) {
                    return value;
                }
            })
            console.log(result);
            setData(result);
            // var result = data.filter(function(value) {
            //     if (value.pname.includes(product.value)) {
            //         return value
            //     }
            // })
            // setData(result);

        })
    </script>
</body>

</html>